﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfShopChargeLedgerLog : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                string strsid = this.Request.QueryString.Get("s");
                this.ExecJScript("$('#txts').val('" + strsid + "');");
                if (string.IsNullOrEmpty(strsid))
                {
                    strsid = "";
                }
                if (strsid == "0")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-2').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 1 + 'px' }, 100);");
                }
                else if (strsid == "1")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-3').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 2 + 'px' }, 100);");
                }
                else if (strsid == "2")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-4').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 3 + 'px' }, 100);");
                }
                dodoloadlist(strsid, "", "", "", "", "");
            }

        }
        private void BindControls()
        {
            if (!IsPostBack)
            { 
            }
        }
        private void ShowStatic()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo odataobj = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
            {
                DataSet ds = odataobj.SelectUserLedgerStatistic(this.oUserInfo.OperDealerId.Split(',')[0]);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    this.ExecJScript("$('#emD1').html('" + ds.Tables[0].Rows[0]["PAY_COUNT"].ToString() + "');");
                    this.ExecJScript("$('#emD2').html('" + ds.Tables[0].Rows[0]["WAIT_PAY_COUNT"].ToString() + "');");
                    this.ExecJScript("$('#emD3').html('" + ds.Tables[0].Rows[0]["COMP_PAY_COUNT"].ToString() + "');");
                    this.ExecJScript("$('#emD4').html('" + ds.Tables[0].Rows[0]["CAC_PAY_COUNT"].ToString() + "');");
                }
                this.ExecJScript("$('#J_SmallTotal').html('" + odataobj.GetShopChargeAmt(this.oUserInfo.OperDealerId.Split(',')[0]).ToString() + "');");
            }
        }

        private void dodoloadlist(string strSid
            , string strDtStart, string strDtEnd
            , string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpOrderRate oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpOrderRate())
            {
                string strSql = @"
    SELECT
    A.* 
    FROM SHP_CHARGE_TRADING_LOG A  
    WHERE
    (A.ACCOUNT_NO='" + this.oUserInfo.OperDealerId.Split(',')[0] + @"')
    ";
                if (strDtStart != string.Empty)
                {
                    strSql += " AND A.INITIAL_DT >='" + strDtStart + "'";
                }
                if (strDtEnd != string.Empty)
                {
                    strSql += " AND A.INITIAL_DT <= '" + strDtEnd + "'";
                }
                if (strSid == "0")
                {
                    strSql += " AND A.TRADING_SID = '待支付'";
                }
                if (strSid == "1")
                {
                    strSql += " AND A.TRADING_SID = '已支付'";
                }

                DataSet ds = oCDbAccess.DataObj.QueryBySQL(strSql);
                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                    , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? "INITIAL_DT ASC" : strSort));
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                updateTableHtml( data);");
                ShowStatic();
            }
        }


        public override void DoAction()
        {
            if (this.strAction == "doloadlist")
            {
                dodoloadlist(this.GetParamHtValue("TRADING_SID")
                    , this.GetParamHtValue("INITIAL_DT_S")
                    , this.GetParamHtValue("INITIAL_DT_E")
                    , this.GetParamHtValue("NEW_PAGE_NUM")
                    , this.GetParamHtValue("PAGE_ROW_COUNT")
                    , this.GetParamHtValue("PAGE_ROW_SORT"));
            }
            else if (this.strAction == "dochongzhi")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpChargeTradingLog objlog = new AppLogic.DataAlc.CBizShpChargeTradingLog())
                    {
                        System.Data.DataRow drLog = objlog.AddTradingLogForChongzhi(this.GetParamHtValue("txtSetScoreQty"), this.oUserInfo.OperDealerId.Split(',')[0], this.oUserInfo.OperDealerId.Split(',')[1], this.oUserInfo.OperType);

                        this.ExecJScript("window.location.href = '/Alipay/default.aspx?ID=" + drLog["CHARGE_TRADING_ID"].ToString() + "';");
                        //this.ExecJScript("window.location.href = '/c_PayCashier.htm?ID=" + drLog["TRADING_NO"].ToString() + "';");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "dobtnGetCashAmt")
            {
                if (this.GetParamHtValue("txtGetCashAmt") == string.Empty || this.GetParamHtValue("TRADING_PSW") == string.Empty || this.GetParamHtValue("VALIDATE_CDE") == string.Empty)
                {
                    Alert("操作不成功：请输入提现金额、交易密码、验证码！");
                    return;
                }
                if (this.GetParamHtValue("VALIDATE_CDE") != (string)GetSession(this.sCurrPageRadom + msBsc.Cls.ValidateCode.VALIDATECODEKEY, "2"))
                {
                    this.ExecJScript("jQuery.getValidateImg('J_CheckCodeContainer');");
                    this.Alert("验证码错误或已过期，请重新录入！");
                    return;
                }
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpChargeTradingLog objlog = new AppLogic.DataAlc.CBizShpChargeTradingLog())
                    {
                        DataSet ds1 = objlog.DataObj.QueryBySQL("SELECT TRADING_PSW FROM SHP_COMMERCIAL_TENANT_INFO A WHERE A.COMM_TENANT_ID='" + this.oUserInfo.OperDealerId.Split(',')[0] + "'");
                        if (ds1.Tables[0].Rows.Count <= 0)
                        {
                            Alert("操作不成功：商家信息未找到！");
                            return;
                        }
                        if (ds1.Tables[0].Rows[0]["TRADING_PSW"].ToString() != msBsc.AppLogic.Cfg.DesCry.encMe(this.GetParamHtValue("TRADING_PSW"), "zbhtpass"))
                        {
                            Alert("操作不成功：商家交易密码不正确！");
                            return;
                        }
                        System.Data.DataRow drLog = objlog.AddTradingLogForGetCash(this.GetParamHtValue("txtGetCashAmt"), this.oUserInfo.OperDealerId.Split(',')[0], this.oUserInfo.OperDealerId.Split(',')[1], "1");
                        dodoloadlist(this.GetParamHtValue("TRADING_SID")
                            , this.GetParamHtValue("INITIAL_DT_S")
                            , this.GetParamHtValue("INITIAL_DT_E")
                            , this.GetParamHtValue("NEW_PAGE_NUM")
                            , this.GetParamHtValue("PAGE_ROW_COUNT")
                            , this.GetParamHtValue("PAGE_ROW_SORT"));
                        this.ExecJScript("layerform && layer.close(layerform);");
                        this.ExecJScript("layer.msg('已成功提交提现申请！',1,{type:1,shade:false});");
                        //this.ExecJScript("window.location.href = '/Alipay/default.aspx?ID=" + drLog["CHARGE_TRADING_ID"].ToString() + "';");
                        //this.ExecJScript("window.location.href = '/c_PayCashier.htm?ID=" + drLog["TRADING_NO"].ToString() + "';");
                    }
                    ts.Complete();
                }
            } 
        }
    }
}